{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "i_hFv44sJIE2"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "from mpl_toolkits.axes_grid1 import ImageGrid\n",
        "from pyts.image import GramianAngularField\n",
        "from pyts.datasets import load_gunpoint\n",
        "from sklearn.metrics.pairwise import cosine_similarity\n",
        "from sklearn.utils import resample\n",
        "from imblearn.combine import SMOTETomek\n",
        "from imblearn.over_sampling import SMOTE\n",
        "from imblearn.under_sampling import TomekLinks, NearMiss\n",
        "from sklearn.model_selection import train_test_split\n",
        "import h5py\n",
        "from PIL import Image\n",
        "from torchvision import datasets, transforms\n",
        "\n",
        "import torch\n",
        "import torch.nn as nn\n",
        "import torch.nn.functional as F\n",
        "import torch.optim as optim\n",
        "import torchvision\n",
        "\n",
        "import torch.nn as nn\n",
        "from torchvision.models import alexnet, vgg16, resnet152, resnet18, vgg19\n",
        "from torch.utils.data import DataLoader, Dataset\n",
        "from sklearn.metrics import classification_report, recall_score, f1_score, precision_score\n",
        "from sklearn.metrics import confusion_matrix\n",
        "from sklearn.metrics.pairwise import cosine_similarity\n",
        "from sklearn.metrics.pairwise import pairwise_distances"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "VxxubohAJIE3"
      },
      "outputs": [],
      "source": [
        "df_hb_normal = pd.read_csv('ptbdb_normal.csv', header=None)\n",
        "df_hb_abnormal = pd.read_csv('ptbdb_abnormal.csv', header=None)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "FUNo6M7NJIE3"
      },
      "outputs": [],
      "source": [
        "df_hb = pd.concat([df_hb_normal, df_hb_abnormal])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "SlvIZwxgJIE3"
      },
      "outputs": [],
      "source": [
        "df_hb = df_hb.reset_index().drop(['index'], axis=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "4sQpHD-8JIE4"
      },
      "outputs": [],
      "source": [
        "x = df_hb.iloc[:,:187]\n",
        "y = df_hb[187]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Yxslh2daJIE4"
      },
      "outputs": [],
      "source": [
        "x_train, x_test, y_train, y_test = train_test_split(x, y, stratify=y, test_size=0.2, random_state=42)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "icTBg1M3JIE4"
      },
      "outputs": [],
      "source": [
        "gasf = GramianAngularField(image_size=150, method='difference')\n",
        "x_gasf_train = gasf.fit_transform(x_train)\n",
        "x_gasf_test = gasf.transform(x_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "y9ScCGGJJIE4"
      },
      "outputs": [],
      "source": [
        "with h5py.File('hb_data_ptb.hdf5', mode='w') as hdf5_file:\n",
        "    hdf5_file.create_dataset(\"x_train\", (len(x_gasf_train), 224, 224), np.float32)\n",
        "    hdf5_file.create_dataset(\"y_train\", (len(y_train),), np.int32)\n",
        "    hdf5_file.create_dataset(\"x_test\", (len(x_gasf_test), 224, 224), np.float32)\n",
        "    hdf5_file.create_dataset(\"y_test\", (len(y_test),), np.int32)\n",
        "\n",
        "    transform = transforms.Compose([transforms.Resize((224, 224))])\n",
        "\n",
        "    for i, (x, y) in enumerate(zip(x_gasf_train, y_train)):\n",
        "        image = Image.fromarray(x.astype(float))\n",
        "        t = np.array(transform(image))\n",
        "        hdf5_file[\"x_train\"][i, ...] = t\n",
        "        hdf5_file[\"y_train\"][i] = y\n",
        "\n",
        "    for i, (x, y) in enumerate(zip(x_gasf_test, y_test)):\n",
        "        image = Image.fromarray(x.astype(float))\n",
        "        t = np.array(transform(image))\n",
        "        hdf5_file[\"x_test\"][i, ...] = t\n",
        "        hdf5_file[\"y_test\"][i] = y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "_XLWnJEeJIE4"
      },
      "outputs": [],
      "source": [
        "def recurrence_plot(s, eps=None, steps=None):\n",
        "    result = []\n",
        "    if eps==None: eps=0.1\n",
        "    if steps==None: steps=10\n",
        "    d = pairwise_distances(s[:, None])\n",
        "    d = d / eps\n",
        "    d[d > steps] = steps\n",
        "    return d/5. - 1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "H6JRrslsJIE4"
      },
      "outputs": [],
      "source": [
        "with h5py.File('hb_data_ptb_rp.hdf5', mode='w') as hdf5_file:\n",
        "    hdf5_file.create_dataset(\"x_train\", (len(x_train), 224, 224), np.float32)\n",
        "    hdf5_file.create_dataset(\"y_train\", (len(y_train),), np.int32)\n",
        "    hdf5_file.create_dataset(\"x_test\", (len(x_test), 224, 224), np.float32)\n",
        "    hdf5_file.create_dataset(\"y_test\", (len(y_test),), np.int32)\n",
        "\n",
        "    transform = transforms.Compose([transforms.Resize((224, 224))])\n",
        "\n",
        "    for i, (x, y) in enumerate(zip(x_train.values, y_train)):\n",
        "        r = recurrence_plot(x, steps=10)\n",
        "        image = Image.fromarray(r.astype(float))\n",
        "        t = np.array(transform(image))\n",
        "        hdf5_file[\"x_train\"][i, ...] = t\n",
        "        hdf5_file[\"y_train\"][i] = y\n",
        "\n",
        "    for i, (x, y) in enumerate(zip(x_test.values, y_test)):\n",
        "        r = recurrence_plot(x, steps=10)\n",
        "        image = Image.fromarray(r.astype(float))\n",
        "        t = np.array(transform(image))\n",
        "        hdf5_file[\"x_test\"][i, ...] = t\n",
        "        hdf5_file[\"y_test\"][i] = y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "XO-17xKDJIE5"
      },
      "outputs": [],
      "source": [
        "with h5py.File('hb_data_ptb_mixed.hdf5', mode='w') as hdf5_file:\n",
        "    hdf5_file.create_dataset(\"x_train\", (len(x_train), 3, 224, 224), np.float32)\n",
        "    hdf5_file.create_dataset(\"y_train\", (len(y_train),), np.int32)\n",
        "    hdf5_file.create_dataset(\"x_test\", (len(x_test), 3, 224, 224), np.float32)\n",
        "    hdf5_file.create_dataset(\"y_test\", (len(y_test),), np.int32)\n",
        "\n",
        "    transform = transforms.Compose([transforms.Resize((224, 224))])\n",
        "\n",
        "    for i, (x1, x2, y) in enumerate(zip(x_gasf_train, x_train.values, y_train)):\n",
        "        image = Image.fromarray(x1.astype(float))\n",
        "        t = np.array(transform(image))\n",
        "        r = recurrence_plot(x2, steps=10)\n",
        "        image = Image.fromarray(r.astype(float))\n",
        "        t2 = np.array(transform(image))\n",
        "        hdf5_file[\"x_train\"][i, 0, ...] = t\n",
        "        hdf5_file[\"x_train\"][i, 1, ...] = t2\n",
        "        hdf5_file[\"x_train\"][i, 2, ...] = (t + t2) / 2\n",
        "        hdf5_file[\"y_train\"][i] = y\n",
        "\n",
        "    for i, (x1, x2, y) in enumerate(zip(x_gasf_test, x_test.values, y_test)):\n",
        "        image = Image.fromarray(x1.astype(float))\n",
        "        t = np.array(transform(image))\n",
        "        r = recurrence_plot(x2, steps=10)\n",
        "        image = Image.fromarray(r.astype(float))\n",
        "        t2 = np.array(transform(image))\n",
        "        hdf5_file[\"x_test\"][i, 0, ...] = t\n",
        "        hdf5_file[\"x_test\"][i, 1, ...] = t2\n",
        "        hdf5_file[\"x_test\"][i, 2, ...] = (t + t2) / 2\n",
        "        hdf5_file[\"y_test\"][i] = y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Lypz1AXKJIE5",
        "outputId": "160c8731-3412-4a72-fb01-cedcde1fa8d5"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "ResNet(\n",
              "  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n",
              "  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "  (relu): ReLU(inplace)\n",
              "  (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)\n",
              "  (layer1): Sequential(\n",
              "    (0): BasicBlock(\n",
              "      (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace)\n",
              "      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    )\n",
              "    (1): BasicBlock(\n",
              "      (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace)\n",
              "      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    )\n",
              "  )\n",
              "  (layer2): Sequential(\n",
              "    (0): BasicBlock(\n",
              "      (conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace)\n",
              "      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (downsample): Sequential(\n",
              "        (0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)\n",
              "        (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      )\n",
              "    )\n",
              "    (1): BasicBlock(\n",
              "      (conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace)\n",
              "      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    )\n",
              "  )\n",
              "  (layer3): Sequential(\n",
              "    (0): BasicBlock(\n",
              "      (conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace)\n",
              "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (downsample): Sequential(\n",
              "        (0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)\n",
              "        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      )\n",
              "    )\n",
              "    (1): BasicBlock(\n",
              "      (conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace)\n",
              "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    )\n",
              "  )\n",
              "  (layer4): Sequential(\n",
              "    (0): BasicBlock(\n",
              "      (conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace)\n",
              "      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (downsample): Sequential(\n",
              "        (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)\n",
              "        (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      )\n",
              "    )\n",
              "    (1): BasicBlock(\n",
              "      (conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace)\n",
              "      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    )\n",
              "  )\n",
              "  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))\n",
              "  (fc): Sequential(\n",
              "    (0): Linear(in_features=512, out_features=256, bias=False)\n",
              "    (1): ReLU()\n",
              "    (2): Linear(in_features=256, out_features=128, bias=True)\n",
              "    (3): ReLU()\n",
              "    (4): Linear(in_features=128, out_features=2, bias=True)\n",
              "  )\n",
              ")"
            ]
          },
          "execution_count": 46,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# loading checkpoint from MIT BIH model\n",
        "ckpt_recall = torch.load('res_net_test_recall_best_cmb.chk')\n",
        "res_net_saved_recall = resnet18(pretrained=True)\n",
        "for param in res_net_saved_recall.parameters():\n",
        "    param.requires_grad = False\n",
        "\n",
        "# MIT BIH model had 2 frozen convolutional layers, 3 trainable FC layers\n",
        "# In this model all convolutional layers are frozen but 3 FCs are trainable\n",
        "# The idea is that the feature generator from MIT BIH will be frozen\n",
        "num_ftrs = res_net_saved_recall.fc.in_features\n",
        "res_net_saved_recall.fc = nn.Sequential(\n",
        "                nn.Linear(in_features=num_ftrs, out_features=256, bias=False),\n",
        "                nn.ReLU(),\n",
        "                nn.Linear(in_features=256, out_features=128, bias=True),\n",
        "                nn.ReLU(),\n",
        "                nn.Linear(in_features=128, out_features=5, bias=True))\n",
        "res_net_saved_recall.load_state_dict(ckpt_recall['net'])\n",
        "res_net_saved_recall.fc = nn.Sequential(\n",
        "                nn.Linear(in_features=num_ftrs, out_features=256, bias=False),\n",
        "                nn.ReLU(),\n",
        "                nn.Linear(in_features=256, out_features=128, bias=True),\n",
        "                nn.ReLU(),\n",
        "                nn.Linear(in_features=128, out_features=2, bias=True))\n",
        "res_net_saved_recall.cuda()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "s0REucLvJIE5"
      },
      "outputs": [],
      "source": [
        "# Function for moving tensor or model to GPU\n",
        "def cuda(xs):\n",
        "    if torch.cuda.is_available():\n",
        "        if not isinstance(xs, (list, tuple)):\n",
        "            return xs.cuda()\n",
        "        else:\n",
        "            return [x.cuda() for x in xs]\n",
        "    else:\n",
        "        return xs\n",
        "\n",
        "# Custom class for defining dataset for training with augmentation\n",
        "class Dataset_Hdf5(Dataset):\n",
        "\n",
        "    def __init__(self, path, data_type):\n",
        "        \"\"\" Intialize the dataset\n",
        "        \"\"\"\n",
        "        self.path = path\n",
        "        self.file = h5py.File(path, 'r')\n",
        "        self.images = self.file['x_{}'.format(data_type)]\n",
        "        self.labels = self.file['y_{}'.format(data_type)]\n",
        "\n",
        "        self.len = self.images.shape[0]\n",
        "        if data_type == 'train':\n",
        "            self.transform = transforms.Compose([transforms.ToTensor()])\n",
        "        else:\n",
        "            self.transform = transforms.Compose([transforms.ToTensor()])\n",
        "\n",
        "    # You must override __getitem__ and __len__\n",
        "    def __getitem__(self, index):\n",
        "        \"\"\" Get a sample from the dataset\n",
        "        \"\"\"\n",
        "        # unsqueeze adds dimension to image -> converts to 1x224x224 since we don't have rgb\n",
        "        return self.transform(self.images[index].astype('float32')), \\\n",
        "                torch.tensor(self.labels[index], dtype=torch.long)\n",
        "\n",
        "    def __len__(self):\n",
        "        \"\"\"\n",
        "        Total number of samples in the dataset\n",
        "        \"\"\"\n",
        "        return self.len\n",
        "\n",
        "# Custom class for defining dataset for training with augmentation\n",
        "class Dataset_Hdf5_3C(Dataset):\n",
        "\n",
        "    def __init__(self, path, data_type):\n",
        "        \"\"\" Intialize the dataset\n",
        "        \"\"\"\n",
        "        self.path = path\n",
        "        self.file = h5py.File(path, 'r')\n",
        "        self.images = self.file['x_{}'.format(data_type)]\n",
        "        self.labels = self.file['y_{}'.format(data_type)]\n",
        "\n",
        "        self.len = self.images.shape[0]\n",
        "\n",
        "    # You must override __getitem__ and __len__\n",
        "    def __getitem__(self, index):\n",
        "        \"\"\" Get a sample from the dataset\n",
        "        \"\"\"\n",
        "        # unsqueeze adds dimension to image -> converts to 1x224x224 since we don't have rgb\n",
        "        return torch.tensor(self.images[index].astype('float32')), \\\n",
        "                torch.tensor(self.labels[index], dtype=torch.long)\n",
        "\n",
        "    def __len__(self):\n",
        "        \"\"\"\n",
        "        Total number of samples in the dataset\n",
        "        \"\"\"\n",
        "        return self.len"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "IpNZWjhTJIE6"
      },
      "outputs": [],
      "source": [
        "hb_train_loader = torch.utils.data.DataLoader(Dataset_Hdf5('/home/asif/heartbeat/hb_data_ptb_rp.hdf5', 'train'),\n",
        "                                                batch_size=64, shuffle=True)\n",
        "hb_test_loader = torch.utils.data.DataLoader(Dataset_Hdf5('/home/asif/heartbeat/hb_data_ptb_rp.hdf5', 'test'),\n",
        "                                                batch_size=64, shuffle=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "olkrLat0JIE6"
      },
      "outputs": [],
      "source": [
        "hb_train_loader2 = torch.utils.data.DataLoader(Dataset_Hdf5_3C('/home/asif/heartbeat/hb_data_ptb_mixed.hdf5', 'train'),\n",
        "                                                batch_size=64, shuffle=True)\n",
        "hb_test_loader2 = torch.utils.data.DataLoader(Dataset_Hdf5_3C('/home/asif/heartbeat/hb_data_ptb_mixed.hdf5', 'test'),\n",
        "                                                batch_size=64, shuffle=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "fkEPc7iEJIE6"
      },
      "outputs": [],
      "source": [
        "class_weights = cuda(torch.tensor([2.0, 1.0]))\n",
        "criterion = nn.CrossEntropyLoss(weight=class_weights)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "xkXodLnBJIE6"
      },
      "outputs": [],
      "source": [
        "optimizer_res_net = torch.optim.Adam([\n",
        "                                          {\"params\": res_net_saved_recall.fc[0].parameters(), \"lr\": 0.001},\n",
        "                                          {\"params\": res_net_saved_recall.fc[2].parameters(), \"lr\": 0.001},\n",
        "                                          {\"params\": res_net_saved_recall.fc[4].parameters(), \"lr\": 0.001},\n",
        "                                           ],\n",
        "                                lr=0.0001, betas=(0.9, 0.999))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "K0dDEUAFJIE6"
      },
      "outputs": [],
      "source": [
        "def train(net, train_loader, criterion, optimizer, test_loader, num_epochs=30):\n",
        "    net.train()\n",
        "    train_acc_max = 0\n",
        "    test_acc_max = 0\n",
        "\n",
        "    scheduler = optim.lr_scheduler.ExponentialLR(optimizer, gamma = 0.95)\n",
        "\n",
        "    for epoch in range(num_epochs):  # loop over the dataset multiple times\n",
        "        net.train()\n",
        "\n",
        "        total = 0\n",
        "        correct = 0\n",
        "\n",
        "        running_loss = 0.0\n",
        "        for i, data in enumerate(train_loader, 0):\n",
        "            # get the inputs; data is a list of [inputs, labels]\n",
        "            inputs, labels = cuda(data)\n",
        "\n",
        "            # zero the parameter gradients\n",
        "            optimizer.zero_grad()\n",
        "\n",
        "            # forward + backward + optimize\n",
        "            # outputs = net(inputs.expand(-1, 3, -1, -1))\n",
        "            outputs = net(inputs)\n",
        "            loss = criterion(outputs, labels)\n",
        "            _, predicted = torch.max(outputs, 1)\n",
        "            total += labels.size(0)\n",
        "            correct += (predicted == labels).sum().item()\n",
        "\n",
        "            loss.backward()\n",
        "            optimizer.step()\n",
        "\n",
        "            # print statistics\n",
        "            running_loss += loss.item()\n",
        "\n",
        "        scheduler.step()\n",
        "\n",
        "        print('End of epoch {}, Loss {}'.format(epoch + 1, running_loss / len(train_loader)))\n",
        "\n",
        "        train_acc = correct / total\n",
        "        print('Train accuracy: {}'.format(train_acc))\n",
        "        test_acc, all_true, all_pred = test(net, test_loader)\n",
        "        print('Test accuracy: {}'.format(test_acc))\n",
        "        precision = precision_score(all_true, all_pred, average='macro')\n",
        "        recall = recall_score(all_true, all_pred, average='macro')\n",
        "        f1 = f1_score(all_true, all_pred, average='macro')\n",
        "        print('Test precision: {}'.format(precision))\n",
        "        print('Test recall: {}'.format(recall))\n",
        "        print('Test f1: {}'.format(f1))\n",
        "\n",
        "    print('Finished Training')\n",
        "\n",
        "def test(net, test_loader):\n",
        "    net.eval()\n",
        "    correct = 0\n",
        "    total = 0\n",
        "    all_true = []\n",
        "    all_pred = []\n",
        "    with torch.no_grad():\n",
        "        for i, data in enumerate(test_loader, 0):\n",
        "            images, labels = cuda(data)\n",
        "            all_true.extend(labels.cpu().tolist())\n",
        "#             outputs = net(images.expand(-1, 3, -1, -1))\n",
        "            outputs = net(images)\n",
        "            _, predicted = torch.max(outputs, 1)\n",
        "            all_pred.extend(predicted.cpu().tolist())\n",
        "#             predicted = predicted.float()\n",
        "            total += labels.size(0)\n",
        "            correct += (predicted == labels).sum().item()\n",
        "\n",
        "    acc = correct / total\n",
        "#     print('Accuracy of the network on the images: %d %%' % (100 * acc))\n",
        "    return acc, all_true, all_pred"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "EltOvFjwJIE6",
        "outputId": "5158ecd0-22f1-4ad9-da70-cd9e41426d89"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "End of epoch 1, Loss 0.5297282012281838\n",
            "Train accuracy: 0.7288033674082982\n",
            "Test accuracy: 0.7475094469254552\n",
            "Test precision: 0.7328944646659404\n",
            "Test recall: 0.7886693936788673\n",
            "Test f1: 0.7295574334876124\n",
            "End of epoch 2, Loss 0.45415015575977474\n",
            "Train accuracy: 0.776393780603041\n",
            "Test accuracy: 0.8110614908965991\n",
            "Test precision: 0.7666388169070044\n",
            "Test recall: 0.7494160602828079\n",
            "Test f1: 0.757004015303564\n",
            "End of epoch 3, Loss 0.4117244554089976\n",
            "Train accuracy: 0.810239670131432\n",
            "Test accuracy: 0.8392305049811062\n",
            "Test precision: 0.7992057175402435\n",
            "Test recall: 0.8373533829103837\n",
            "Test f1: 0.812732993021481\n",
            "End of epoch 4, Loss 0.37393098725722385\n",
            "Train accuracy: 0.8356670389141826\n",
            "Test accuracy: 0.8570937822054276\n",
            "Test precision: 0.8183661723046516\n",
            "Test recall: 0.8493423768522297\n",
            "Test f1: 0.8307207998076526\n",
            "End of epoch 5, Loss 0.36926299243510424\n",
            "Train accuracy: 0.8319731981788506\n",
            "Test accuracy: 0.8461009962212298\n",
            "Test precision: 0.8258977983109367\n",
            "Test recall: 0.7725381324984505\n",
            "Test f1: 0.7923548218630186\n",
            "End of epoch 6, Loss 0.32925393669814856\n",
            "Train accuracy: 0.857486470234516\n",
            "Test accuracy: 0.8722088629336998\n",
            "Test precision: 0.8355595084261667\n",
            "Test recall: 0.8674121649991355\n",
            "Test f1: 0.8484302356829969\n",
            "End of epoch 7, Loss 0.32912645063229967\n",
            "Train accuracy: 0.8522463705867194\n",
            "Test accuracy: 0.8660254208175885\n",
            "Test precision: 0.8298925478019283\n",
            "Test recall: 0.8798583725664768\n",
            "Test f1: 0.8461189593738547\n",
            "End of epoch 8, Loss 0.31807686806052593\n",
            "Train accuracy: 0.861438020788592\n",
            "Test accuracy: 0.880109927859842\n",
            "Test precision: 0.8449709212179902\n",
            "Test recall: 0.8728831450181651\n",
            "Test f1: 0.856727276829816\n",
            "End of epoch 9, Loss 0.2989256664455592\n",
            "Train accuracy: 0.8700283480800618\n",
            "Test accuracy: 0.8667124699416008\n",
            "Test precision: 0.8323352452065174\n",
            "Test recall: 0.889458388561603\n",
            "Test f1: 0.848838317326738\n",
            "End of epoch 10, Loss 0.2779145413538912\n",
            "Train accuracy: 0.8808521604673138\n",
            "Test accuracy: 0.8945379594641016\n",
            "Test precision: 0.8636714036170835\n",
            "Test recall: 0.881733095445035\n",
            "Test f1: 0.8718879488857463\n",
            "End of epoch 11, Loss 0.27821084070991686\n",
            "Train accuracy: 0.8812816768318873\n",
            "Test accuracy: 0.8976296805221573\n",
            "Test precision: 0.8712209559456805\n",
            "Test recall: 0.875129813386274\n",
            "Test f1: 0.8731401212036127\n",
            "End of epoch 12, Loss 0.2689893488045577\n",
            "Train accuracy: 0.8836010652005841\n",
            "Test accuracy: 0.896942631398145\n",
            "Test precision: 0.8631342660814544\n",
            "Test recall: 0.9043079814503581\n",
            "Test f1: 0.8789987363660547\n",
            "End of epoch 13, Loss 0.2644710114614649\n",
            "Train accuracy: 0.8866076797525986\n",
            "Test accuracy: 0.8986602542081759\n",
            "Test precision: 0.8703661843194337\n",
            "Test recall: 0.8815460936020671\n",
            "Test f1: 0.87565983924805\n",
            "End of epoch 14, Loss 0.27707102088319074\n",
            "Train accuracy: 0.882484322652693\n",
            "Test accuracy: 0.9127447612504294\n",
            "Test precision: 0.8854869492001123\n",
            "Test recall: 0.9038445932357082\n",
            "Test f1: 0.8938931678929659\n",
            "End of epoch 15, Loss 0.2523145760808672\n",
            "Train accuracy: 0.8930504252212009\n",
            "Test accuracy: 0.9117141875644108\n",
            "Test precision: 0.8852026170878444\n",
            "Test recall: 0.9000895609455472\n",
            "Test f1: 0.8921435441064507\n",
            "End of epoch 16, Loss 0.24380310100841\n",
            "Train accuracy: 0.8973455888669358\n",
            "Test accuracy: 0.9113706630024047\n",
            "Test precision: 0.8848664343786294\n",
            "Test recall: 0.8994715139739773\n",
            "Test f1: 0.8916851268561143\n",
            "End of epoch 17, Loss 0.24984503668415678\n",
            "Train accuracy: 0.895026200498239\n",
            "Test accuracy: 0.9062177945723119\n",
            "Test precision: 0.8746813805041653\n",
            "Test recall: 0.9073088317794931\n",
            "Test f1: 0.8882960039300767\n",
            "End of epoch 18, Loss 0.24296276815808737\n",
            "Train accuracy: 0.8957134266815565\n",
            "Test accuracy: 0.895225008588114\n",
            "Test precision: 0.8615745695850279\n",
            "Test recall: 0.9171852341463014\n",
            "Test f1: 0.8798272695071314\n",
            "End of epoch 19, Loss 0.22755458676716783\n",
            "Train accuracy: 0.9041319474271969\n",
            "Test accuracy: 0.9117141875644108\n",
            "Test precision: 0.8804097442958807\n",
            "Test recall: 0.9171975833246105\n",
            "Test f1: 0.8953876439381767\n",
            "End of epoch 20, Loss 0.2209459048296724\n",
            "Train accuracy: 0.9099733699853965\n",
            "Test accuracy: 0.8873239436619719\n",
            "Test precision: 0.853310853326838\n",
            "Test recall: 0.90943318447673\n",
            "Test f1: 0.8711627103641093\n",
            "End of epoch 21, Loss 0.2248838973733095\n",
            "Train accuracy: 0.906107722704235\n",
            "Test accuracy: 0.9172105805565098\n",
            "Test precision: 0.8900239667809269\n",
            "Test recall: 0.9114990255910258\n",
            "Test f1: 0.8997105899490585\n",
            "End of epoch 22, Loss 0.21565796574065974\n",
            "Train accuracy: 0.9087707241645907\n",
            "Test accuracy: 0.9055307454482996\n",
            "Test precision: 0.8724804505104908\n",
            "Test recall: 0.9193789774645138\n",
            "Test f1: 0.8898610228475174\n",
            "End of epoch 23, Loss 0.2111920224768775\n",
            "Train accuracy: 0.9097156601666524\n",
            "Test accuracy: 0.9020954998282377\n",
            "Test precision: 0.8686794510474097\n",
            "Test recall: 0.9150993991242669\n",
            "Test f1: 0.8858559691328818\n",
            "End of epoch 24, Loss 0.20809642250066276\n",
            "Train accuracy: 0.9138390172665578\n",
            "Test accuracy: 0.9151494331844727\n",
            "Test precision: 0.8841149048570424\n",
            "Test recall: 0.9226176964901283\n",
            "Test f1: 0.8996513000013258\n",
            "End of epoch 25, Loss 0.20036622533922668\n",
            "Train accuracy: 0.9182200841852075\n",
            "Test accuracy: 0.9206458261765716\n",
            "Test precision: 0.9017244697254119\n",
            "Test recall: 0.9001912946525705\n",
            "Test f1: 0.9009530989081496\n",
            "End of epoch 26, Loss 0.21354686579370236\n",
            "Train accuracy: 0.913237694356155\n",
            "Test accuracy: 0.923394022672621\n",
            "Test precision: 0.8990423542130308\n",
            "Test recall: 0.915020305577477\n",
            "Test f1: 0.906479312091997\n",
            "End of epoch 27, Loss 0.19716024861394704\n",
            "Train accuracy: 0.9202817627351602\n",
            "Test accuracy: 0.9271727928546891\n",
            "Test precision: 0.9021683823241614\n",
            "Test recall: 0.9229593570900161\n",
            "Test f1: 0.9116246699164265\n",
            "End of epoch 28, Loss 0.19713045374213994\n",
            "Train accuracy: 0.9208830856455631\n",
            "Test accuracy: 0.9216763998625902\n",
            "Test precision: 0.8948145303688972\n",
            "Test recall: 0.9183931013961628\n",
            "Test f1: 0.9053500735856159\n",
            "End of epoch 29, Loss 0.19267749567362633\n",
            "Train accuracy: 0.9212266987372218\n",
            "Test accuracy: 0.9230504981106149\n",
            "Test precision: 0.9063548052381747\n",
            "Test recall: 0.9007158407026565\n",
            "Test f1: 0.9034722880151587\n",
            "End of epoch 30, Loss 0.19224871478074199\n",
            "Train accuracy: 0.9229447641955159\n",
            "Test accuracy: 0.9151494331844727\n",
            "Test precision: 0.8843119560228379\n",
            "Test recall: 0.9214771616648574\n",
            "Test f1: 0.8994581636292982\n",
            "Finished Training\n"
          ]
        }
      ],
      "source": [
        "train(res_net_saved_recall, hb_train_loader2, criterion, optimizer_res_net, hb_test_loader2, 30)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6PChpPfPJIE6"
      },
      "source": [
        "## MTF"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "YdrI2kOLJIE7",
        "outputId": "eb9ea9cf-dab3-4b28-c38b-5d5b6e77ef37"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "ResNet(\n",
              "  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n",
              "  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "  (relu): ReLU(inplace=True)\n",
              "  (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)\n",
              "  (layer1): Sequential(\n",
              "    (0): BasicBlock(\n",
              "      (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace=True)\n",
              "      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    )\n",
              "    (1): BasicBlock(\n",
              "      (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace=True)\n",
              "      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    )\n",
              "  )\n",
              "  (layer2): Sequential(\n",
              "    (0): BasicBlock(\n",
              "      (conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace=True)\n",
              "      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (downsample): Sequential(\n",
              "        (0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)\n",
              "        (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      )\n",
              "    )\n",
              "    (1): BasicBlock(\n",
              "      (conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace=True)\n",
              "      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    )\n",
              "  )\n",
              "  (layer3): Sequential(\n",
              "    (0): BasicBlock(\n",
              "      (conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace=True)\n",
              "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (downsample): Sequential(\n",
              "        (0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)\n",
              "        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      )\n",
              "    )\n",
              "    (1): BasicBlock(\n",
              "      (conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace=True)\n",
              "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    )\n",
              "  )\n",
              "  (layer4): Sequential(\n",
              "    (0): BasicBlock(\n",
              "      (conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace=True)\n",
              "      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (downsample): Sequential(\n",
              "        (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)\n",
              "        (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      )\n",
              "    )\n",
              "    (1): BasicBlock(\n",
              "      (conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "      (relu): ReLU(inplace=True)\n",
              "      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
              "      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    )\n",
              "  )\n",
              "  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))\n",
              "  (fc): Sequential(\n",
              "    (0): Linear(in_features=512, out_features=256, bias=False)\n",
              "    (1): ReLU()\n",
              "    (2): Linear(in_features=256, out_features=128, bias=True)\n",
              "    (3): ReLU()\n",
              "    (4): Linear(in_features=128, out_features=2, bias=True)\n",
              "  )\n",
              ")"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# loading checkpoint from MIT BIH model\n",
        "ckpt_recall_mtf = torch.load('res_net_mtf_gaf_rp_test_recall_best.chk')\n",
        "res_net_saved_recall = resnet18(pretrained=True)\n",
        "for param in res_net_saved_recall.parameters():\n",
        "    param.requires_grad = False\n",
        "\n",
        "# MIT BIH model had 2 frozen convolutional layers, 3 trainable FC layers\n",
        "# In this model all convolutional layers are frozen but 3 FCs are trainable\n",
        "# The idea is that the feature generator from MIT BIH will be frozen\n",
        "num_ftrs = res_net_saved_recall.fc.in_features\n",
        "res_net_saved_recall.fc = nn.Sequential(\n",
        "                nn.Linear(in_features=num_ftrs, out_features=256, bias=False),\n",
        "                nn.ReLU(),\n",
        "                nn.Linear(in_features=256, out_features=128, bias=True),\n",
        "                nn.ReLU(),\n",
        "                nn.Linear(in_features=128, out_features=5, bias=True))\n",
        "res_net_saved_recall.load_state_dict(ckpt_recall_mtf['net'])\n",
        "res_net_saved_recall.fc = nn.Sequential(\n",
        "                nn.Linear(in_features=num_ftrs, out_features=256, bias=False),\n",
        "                nn.ReLU(),\n",
        "                nn.Linear(in_features=256, out_features=128, bias=True),\n",
        "                nn.ReLU(),\n",
        "                nn.Linear(in_features=128, out_features=2, bias=True))\n",
        "res_net_saved_recall.cuda()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "vFggL8rEJIE7"
      },
      "outputs": [],
      "source": [
        "def get_quantiles(min_value=0, max_val=1, k=10):\n",
        "    c = (max_val - min_value)/k\n",
        "    b = min_value + c\n",
        "    d = []\n",
        "    for i in range(1, k):\n",
        "        d.append(b)\n",
        "        b += c\n",
        "    d.append(max_val)\n",
        "    return d\n",
        "\n",
        "quantiles = get_quantiles()\n",
        "\n",
        "def value_to_quantile(x):\n",
        "    for i, k in enumerate(quantiles):\n",
        "        if x <= k:\n",
        "            return i\n",
        "    return 0\n",
        "\n",
        "def get_mtf(x, size=10):\n",
        "    q = np.vectorize(value_to_quantile)(x)\n",
        "    r = np.zeros((q.shape[0], q.shape[0]))\n",
        "    y = np.zeros((size, size))\n",
        "    for i in range(x.shape[0] - 1):\n",
        "        y[q[i], q[i + 1]] += 1\n",
        "    y = y / y.sum(axis=1, keepdims=True)\n",
        "    y[np.isnan(y)] = 0\n",
        "\n",
        "    for i in range(r.shape[0]):\n",
        "        for j in range(r.shape[1]):\n",
        "            r[i, j] = y[q[i], q[j]]\n",
        "    return r / 5. - 1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "kOTT_ieyJIE7",
        "outputId": "f8ea062f-6465-4e0c-d2bb-d5e29f3bcaeb"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "np.seterr(divide='ignore', invalid='ignore')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "HPqcFIQzJIE7"
      },
      "outputs": [],
      "source": [
        "# 3 channels, this time with GAF, RP and MTF\n",
        "with h5py.File('/home/asif/heartbeat/hb_data_ptb_balanced_gaf_rp_mtf.hdf5', mode='w') as hdf5_file:\n",
        "    hdf5_file.create_dataset(\"x_train\", (len(x_gasf_train), 3, 224, 224), np.float32)\n",
        "    hdf5_file.create_dataset(\"y_train\", (len(y_train),), np.int32)\n",
        "    hdf5_file.create_dataset(\"x_test\", (len(x_gasf_test), 3, 224, 224), np.float32)\n",
        "    hdf5_file.create_dataset(\"y_test\", (len(y_test),), np.int32)\n",
        "\n",
        "    transform = transforms.Compose([transforms.Resize((224, 224))])\n",
        "\n",
        "    for i, (x1, x2, y) in enumerate(zip(x_gasf_train, x_train.values, y_train)):\n",
        "        image = Image.fromarray(x1.astype(float))\n",
        "        t = np.array(transform(image))\n",
        "        r = recurrence_plot(x2, steps=10)\n",
        "        mtf = get_mtf(x2)\n",
        "        image = Image.fromarray(r.astype(float))\n",
        "        t2 = np.array(transform(image))\n",
        "        image = Image.fromarray(mtf.astype(float))\n",
        "        t3 = np.array(transform(image))\n",
        "        hdf5_file[\"x_train\"][i, 0, ...] = t\n",
        "        hdf5_file[\"x_train\"][i, 1, ...] = t2\n",
        "        hdf5_file[\"x_train\"][i, 2, ...] = t3\n",
        "        hdf5_file[\"y_train\"][i] = y\n",
        "\n",
        "    for i, (x1, x2, y) in enumerate(zip(x_gasf_test, x_test.values, y_test)):\n",
        "        image = Image.fromarray(x1.astype(float))\n",
        "        t = np.array(transform(image))\n",
        "        r = recurrence_plot(x2, steps=10)\n",
        "        mtf = get_mtf(x2)\n",
        "        image = Image.fromarray(r.astype(float))\n",
        "        t2 = np.array(transform(image))\n",
        "        image = Image.fromarray(mtf.astype(float))\n",
        "        t3 = np.array(transform(image))\n",
        "        hdf5_file[\"x_test\"][i, 0, ...] = t\n",
        "        hdf5_file[\"x_test\"][i, 1, ...] = t2\n",
        "        hdf5_file[\"x_test\"][i, 2, ...] = t3\n",
        "        hdf5_file[\"y_test\"][i] = y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "zCPWRHKjJIE7"
      },
      "outputs": [],
      "source": [
        "hb_train_loader3 = torch.utils.data.DataLoader(Dataset_Hdf5_3C('/home/asif/heartbeat/hb_data_ptb_balanced_gaf_rp_mtf.hdf5', 'train'),\n",
        "                                                batch_size=64, shuffle=True)\n",
        "hb_test_loader3 = torch.utils.data.DataLoader(Dataset_Hdf5_3C('/home/asif/heartbeat/hb_data_ptb_balanced_gaf_rp_mtf.hdf5', 'test'),\n",
        "                                                batch_size=64, shuffle=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "MTrGELMRJIE7",
        "outputId": "74a06259-5c77-42fc-98af-87a51a5b9072"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "End of epoch 1, Loss 0.36358735792256974\n",
            "Train accuracy: 0.8420238811098703\n",
            "Test accuracy: 0.8962555822741326\n",
            "Test precision: 0.8628725938324857\n",
            "Test recall: 0.8977493916559542\n",
            "Test f1: 0.8770307394476523\n",
            "End of epoch 2, Loss 0.2538518090169508\n",
            "Train accuracy: 0.899836783781462\n",
            "Test accuracy: 0.9203023016145654\n",
            "Test precision: 0.890689838786677\n",
            "Test recall: 0.9254253703871409\n",
            "Test f1: 0.9051651989678364\n",
            "End of epoch 3, Loss 0.19198271791849816\n",
            "Train accuracy: 0.9252641525642127\n",
            "Test accuracy: 0.947440742013054\n",
            "Test precision: 0.9312358490674961\n",
            "Test recall: 0.939274679832851\n",
            "Test f1: 0.9351330166177378\n",
            "End of epoch 4, Loss 0.16522728530997102\n",
            "Train accuracy: 0.9367751911347822\n",
            "Test accuracy: 0.9440054963929921\n",
            "Test precision: 0.9235311721824763\n",
            "Test recall: 0.9410779538940488\n",
            "Test f1: 0.9316905225500182\n",
            "End of epoch 5, Loss 0.13975154940079856\n",
            "Train accuracy: 0.944506485697105\n",
            "Test accuracy: 0.9367914805908623\n",
            "Test precision: 0.9102379445488396\n",
            "Test recall: 0.9436862767697842\n",
            "Test f1: 0.9244887003164677\n",
            "End of epoch 6, Loss 0.12235010992530938\n",
            "Train accuracy: 0.9518941671677691\n",
            "Test accuracy: 0.9508759876331158\n",
            "Test precision: 0.939825046203836\n",
            "Test recall: 0.9374714057716531\n",
            "Test f1: 0.9386382889777165\n",
            "End of epoch 7, Loss 0.1082827462251861\n",
            "Train accuracy: 0.9582510093634568\n",
            "Test accuracy: 0.9491583648230848\n",
            "Test precision: 0.9289855854458304\n",
            "Test recall: 0.9488279453672351\n",
            "Test f1: 0.9381303601073028\n",
            "End of epoch 8, Loss 0.09578531783503982\n",
            "Train accuracy: 0.9661541104716089\n",
            "Test accuracy: 0.9426313981449673\n",
            "Test precision: 0.9169011489905576\n",
            "Test recall: 0.9511516490857491\n",
            "Test f1: 0.9314879760754661\n",
            "End of epoch 9, Loss 0.08240064882396997\n",
            "Train accuracy: 0.9706210806631733\n",
            "Test accuracy: 0.9577464788732394\n",
            "Test precision: 0.9391059450508836\n",
            "Test recall: 0.9597169803553975\n",
            "Test f1: 0.9485994490044914\n",
            "End of epoch 10, Loss 0.07309618521645501\n",
            "Train accuracy: 0.97440082467142\n",
            "Test accuracy: 0.9591205771212642\n",
            "Test precision: 0.951430128419452\n",
            "Test recall: 0.9462216806878845\n",
            "Test f1: 0.9487792280916751\n",
            "End of epoch 11, Loss 0.06807686936028384\n",
            "Train accuracy: 0.9756893737651404\n",
            "Test accuracy: 0.9649604946753693\n",
            "Test precision: 0.9563483009294815\n",
            "Test recall: 0.9563483009294815\n",
            "Test f1: 0.9563483009294815\n",
            "End of epoch 12, Loss 0.05607558249311706\n",
            "Train accuracy: 0.9793832145004725\n",
            "Test accuracy: 0.9628993473033322\n",
            "Test precision: 0.954091987410538\n",
            "Test recall: 0.953400375650243\n",
            "Test f1: 0.9537453567040013\n",
            "End of epoch 13, Loss 0.05942286863273194\n",
            "Train accuracy: 0.9786959883171549\n",
            "Test accuracy: 0.9649604946753693\n",
            "Test precision: 0.9554207839647825\n",
            "Test recall: 0.9574888357547524\n",
            "Test f1: 0.9564474169027241\n",
            "End of epoch 14, Loss 0.05326511925035207\n",
            "Train accuracy: 0.9797268275921313\n",
            "Test accuracy: 0.9615252490553075\n",
            "Test precision: 0.9599856369178803\n",
            "Test recall: 0.9433246222621577\n",
            "Test f1: 0.9512001197425535\n",
            "End of epoch 15, Loss 0.04008988990540524\n",
            "Train accuracy: 0.9859977665149042\n",
            "Test accuracy: 0.9618687736173136\n",
            "Test precision: 0.9478198126626631\n",
            "Test recall: 0.9587696219622491\n",
            "Test f1: 0.9530778480991519\n",
            "End of epoch 16, Loss 0.039975024304342466\n",
            "Train accuracy: 0.9860836697878189\n",
            "Test accuracy: 0.9680522157334249\n",
            "Test precision: 0.9623448210318732\n",
            "Test recall: 0.9577287626476167\n",
            "Test f1: 0.96000124108249\n",
            "End of epoch 17, Loss 0.031389762131896405\n",
            "Train accuracy: 0.9883171548836011\n",
            "Test accuracy: 0.9656475437993817\n",
            "Test precision: 0.9653141329311182\n",
            "Test recall: 0.948460116270454\n",
            "Test f1: 0.9564286783415656\n",
            "End of epoch 18, Loss 0.029528486165653015\n",
            "Train accuracy: 0.9903788334335538\n",
            "Test accuracy: 0.9587770525592579\n",
            "Test precision: 0.9398097448783066\n",
            "Test recall: 0.9619512995451974\n",
            "Test f1: 0.9499411882046238\n",
            "End of epoch 19, Loss 0.03320701088971244\n",
            "Train accuracy: 0.9877158319731982\n",
            "Test accuracy: 0.9680522157334249\n",
            "Test precision: 0.9613380607277444\n",
            "Test recall: 0.9588692974728876\n",
            "Test f1: 0.9600934326918017\n",
            "End of epoch 20, Loss 0.023919517734052185\n",
            "Train accuracy: 0.9915814792543596\n",
            "Test accuracy: 0.9556853315012024\n",
            "Test precision: 0.9333149359386241\n",
            "Test recall: 0.9639924423028747\n",
            "Test f1: 0.9467579802630264\n",
            "End of epoch 21, Loss 0.02461839620833756\n",
            "Train accuracy: 0.9919250923460183\n",
            "Test accuracy: 0.9694263139814496\n",
            "Test precision: 0.9592845820196143\n",
            "Test recall: 0.96514326811007\n",
            "Test f1: 0.962154739915086\n",
            "End of epoch 22, Loss 0.022715774712896045\n",
            "Train accuracy: 0.9924405119835066\n",
            "Test accuracy: 0.9649604946753693\n",
            "Test precision: 0.9525244309069723\n",
            "Test recall: 0.9612906185056553\n",
            "Test f1: 0.9567713046790103\n",
            "End of epoch 23, Loss 0.015918358955097352\n",
            "Train accuracy: 0.9951035134438622\n",
            "Test accuracy: 0.9673651666094125\n",
            "Test precision: 0.9582488066825776\n",
            "Test recall: 0.9606746297304704\n",
            "Test f1: 0.9594516229541247\n",
            "End of epoch 24, Loss 0.015230941878514016\n",
            "Train accuracy: 0.9952753199896917\n",
            "Test accuracy: 0.9615252490553075\n",
            "Test precision: 0.9437109580063558\n",
            "Test recall: 0.964234427392124\n",
            "Test f1: 0.9531797319730939\n",
            "End of epoch 25, Loss 0.015027948441226957\n",
            "Train accuracy: 0.9949317068980328\n",
            "Test accuracy: 0.9708004122294744\n",
            "Test precision: 0.9609748520795954\n",
            "Test recall: 0.9668550994461688\n",
            "Test f1: 0.9638556504807001\n",
            "End of epoch 26, Loss 0.01677134183330134\n",
            "Train accuracy: 0.9940726741688858\n",
            "Test accuracy: 0.9677086911714188\n",
            "Test precision: 0.9607431571833329\n",
            "Test recall: 0.9586314287764082\n",
            "Test f1: 0.9596797769696399\n",
            "End of epoch 27, Loss 0.010486663457253758\n",
            "Train accuracy: 0.9964779658104974\n",
            "Test accuracy: 0.9704568876674682\n",
            "Test precision: 0.9631956262738766\n",
            "Test recall: 0.9631956262738766\n",
            "Test f1: 0.9631956262738766\n",
            "End of epoch 28, Loss 0.01363411705960471\n",
            "Train accuracy: 0.9962202559917532\n",
            "Test accuracy: 0.9697698385434559\n",
            "Test precision: 0.9694560095386225\n",
            "Test recall: 0.9547361451040213\n",
            "Test f1: 0.9617490255384773\n",
            "End of epoch 29, Loss 0.014805318040429144\n",
            "Train accuracy: 0.99433038398763\n",
            "Test accuracy: 0.9683957402954311\n",
            "Test precision: 0.9606278792697285\n",
            "Test recall: 0.9606278792697285\n",
            "Test f1: 0.9606278792697285\n",
            "End of epoch 30, Loss 0.008498535618335674\n",
            "Train accuracy: 0.9972510952667296\n",
            "Test accuracy: 0.9683957402954311\n",
            "Test precision: 0.9564431448588602\n",
            "Test recall: 0.9659503751209926\n",
            "Test f1: 0.9610381372699637\n",
            "Finished Training\n"
          ]
        }
      ],
      "source": [
        "train(res_net_saved_recall, hb_train_loader3, criterion, optimizer_res_net, hb_test_loader3, 30)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "xl2KgEowJIE8"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.3"
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}